package com.boo.esspringbootstarter.auto.autoindex;

import com.boo.esspringbootstarter.annotation.EnableESTools;
import com.boo.esspringbootstarter.auto.util.GetBasePackage;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.core.type.AnnotationMetadata;

/**
 * program: esdemo
 * description: spring初始化完成后通过读取启动类EnableESTools注解上entity的路径（或者不配置，取启动类所在包），得到路径后委托ESEntityScanner扫描相关路径
 * ESCRegistrar进行调用
 * author: X-Pacific zhang
 * create: 2019-01-30 17:22
 **/
public class ESIndexProcessor {


    /**
     * 扫描ESMetaData注解的类entitypath或根路径的entity托管给spring
     * @param beanFactory
     * @throws BeansException
     */
    public void scan(AnnotationMetadata annotationMetadata,BeanFactory beanFactory,ApplicationContext applicationContext){
        GetBasePackage getBasePackage = new GetBasePackage(EnableESTools.class);
        ESEntityScanner scanner = new ESEntityScanner((BeanDefinitionRegistry) beanFactory);
        scanner.setResourceLoader(applicationContext);
        scanner.scan(getBasePackage.getEntityPackage(annotationMetadata).toArray(String[]::new));
    }
}
